package beautiful.butterfly.distributed_service_container.application;

import java.io.File;

public class Paths
{
    /**
     * 获取项目的lib根路径
     */
    public static String getClassPath(Class clazz)
    {
        String path = clazz.getResource("").getFile();
        path = Environment.pathFix(path);

        // 公共部分:/beautiful/butterfly/distributed_service_container/application/
        String $ = File.separator + BootStrap.class.getPackage().getName().replace(".", File.separator) + File.separator;
        //
        path = path.replace($, "");// xyz/classes or lib/xyz.jar!(如果是jar文件这种格式则需要去掉lib/xyz.jar)
        //

        if (path.contains(".jar"))
        {
            //jar path:file:\E:\czh\p\server_product\service_container\lib\xiaomi-0.0.0-RELEASE.jar!
            //递归父级目录两层
            int lastIndexOf = path.lastIndexOf(File.separator);
            path = path.substring(0, lastIndexOf);
            lastIndexOf = path.lastIndexOf(File.separator);
            path = path.substring(0, lastIndexOf);

            System.out.println("jar path:" + path);
        }
        return path.replace("file:\\", "");//去掉file:\
    }
}
